package Chapter9;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class Example14 {
	public static void main(String[] args) {
		final Frame frame=new Frame("验证码");
		final Panel panel=new MyPanel();
		frame.add(panel);
		frame.setSize(200, 100);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}
}
class MyPanel extends Panel{
	public void paint(Graphics g) {
		int width=160;
		int height=40;
		g.setColor(Color.LIGHT_GRAY);
		g.fillRect(20, 30, width, height);
		g.setColor(Color.BLACK);
		g.drawRect(20, 30, width, height);
		Random r=new Random();
		for(int i=0;i<100;i++) {
			int x=r.nextInt(width)+20-2;
			int y=r.nextInt(height)+30-2;
			g.drawOval(x, y, 2, 2);
		}
		g.setFont(new Font("黑体",Font.BOLD,30));
		g.setColor(Color.BLUE);
		char[] chars=("0123456789abcdefghijklmnopkrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
		StringBuilder sb=new StringBuilder();
		for(int i=0;i<4;i++) {
			int pos=r.nextInt(chars.length);
			char c=chars[pos];
			sb.append(c+" ");
		}
		g.drawString(sb.toString(), 40, 60);
	}
}
